import { _decorator, Component, instantiate, Label, Node, Prefab, Sprite, SpriteFrame, tween, v3, Vec3 } from 'cc';
import { UIManager } from '../common/UIManager';
import VillageApi from '../api/VillageApi';
import { UIBase } from './UIBase';
import { raceName } from '../settings/const'
import ResManager from '../common/ResManager';
import { UserInfo } from '../common/UserInfo';
import { MapZone } from '../entity/MapZone';
import { PrefabEnum } from '../common/PrefabEnum';
import { PanelVillage } from './PanelVillage';
import { PanelOasis } from './PanelOasis';
import { PanelOasisOccupy } from './PanelOasisOccupy';
import { PanelSpace } from './PanelSpace';
import { Village } from '../entity/Village';
import { TilesManager } from '../common/TilesManager';
const { ccclass, property } = _decorator;

@ccclass('VillageInformation')
export class VillageInformation extends Component {
    @property(Node)
    public mask: Node;
    @property(Node)
    public close: Node;
    @property(Label)
    public villageName: Label;//名称
    @property(Label)
    public coordinate: Label;//坐标
    @property(Sprite)
    public cover: Sprite;//图片
    @property(Node)
    public panelPos: Node
    @property(Node)
    public panelVillage: Node
    @property(Node)
    public panelSpace: Node
    @property(Node)
    public panelOasis: Node
    @property(Node)
    public panelOasisOccupy: Node

   

   

    start() {

        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)

    }

    onClose() {

        UIManager.instance.closeUI(PrefabEnum.VillageInformation)
    }

    async setData(zone: MapZone) {
        await UIManager.instance.showLoading()
        let res = await VillageApi.queryThird(zone.zoneId)
        let data:Village =  Village.createByVillageObj(res.data)
        console.log(data)
        UIManager.instance.closeLoading()

        this.villageName.string = data.name
        this.coordinate.string = `( ${zone.x} , ${zone.y} )`

        if (zone.typeId < 20) {
            if (zone.uid == 0) {
                console.log('点击了无人的绿洲')
                // 绿洲
                this.updateOasis(zone, data)


            } else {
                console.log('点击了被占领的绿洲')
                // 被占领的绿洲
                this.updateOasisOccupy(zone, data)
                
            }

        }

        if (zone.typeId > 20) {
            if (zone.uid == 0) {
                // 空地
                this.updateSpace(zone, data)
                
            } else {
                // 村庄
                this.updateVillage(zone, data)
               

            }

        }
    }

    async updateVillage(zone:MapZone, data:Village) {

        this.cover.spriteFrame = await data.getImg()
        let node = instantiate(this.panelVillage)
        this.node.addChild(node)
        node.position = this.panelPos.position
        node.getComponent(PanelVillage).setData(zone, data)



    }

    async updateSpace(zone:MapZone, data:Village) {
        this.cover.spriteFrame = await TilesManager.getTilesImg(zone.x, zone.y)

        this.cover.node.scale = v3(2,2,2)
        let node = instantiate(this.panelSpace)
        this.node.addChild(node)
        
        node.position = this.panelPos.position
        node.getComponent(PanelSpace).setData(zone, data)
    }

    async updateOasis(zone:MapZone, data:Village) {
        this.cover.spriteFrame = await zone.getImg()
        let node = instantiate(this.panelOasis)
        this.node.addChild(node)
        node.position = this.panelPos.position
        node.getComponent(PanelOasis).setData(zone, data)
    }

    async updateOasisOccupy(zone:MapZone, data:Village) {
        this.cover.spriteFrame = await zone.getImg()
        let node = instantiate(this.panelOasisOccupy)
        this.node.addChild(node)
        node.position = this.panelPos.position
        node.getComponent(PanelOasisOccupy).setData(zone, data)
    }


}


